package com.netease.yunxin.kit.conversationkit.ui.page

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.angcyo.tablayout.delegate2.ViewPager2Delegate.Companion.install
import com.jeremyliao.liveeventbus.LiveEventBus
import com.netease.yunxin.kit.common.ui.fragments.BaseFragment
import com.netease.yunxin.kit.conversationkit.ui.databinding.FragmentConversationCallSubBinding

/**
 * Author: Sakura

 * Date: 2023/4/21

 * Description:
 */
class ConversationCallSubFragment : BaseFragment() {

    private lateinit var binding: FragmentConversationCallSubBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentConversationCallSubBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        binding.viewPager.adapter = object : FragmentStateAdapter(
            childFragmentManager,
            lifecycle
        ) {
            override fun createFragment(position: Int): Fragment {
                return if (position == 0) {
                    ConversationItemFragment()
                } else ConversationFriendsFragment()
            }

            override fun getItemCount(): Int {
                return 2
            }
        }
        install(binding.viewPager, binding.tab, null)
        binding.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                if (position != 0){
                    LiveEventBus.get<Boolean>("HIDE").post(true)
                }else{
                    LiveEventBus.get<Boolean>("HIDE").post(false)
                }
            }
        })
    }
}